//package com.outfood.aspect;
//
//import com.outfood.annotation.HubSelect;
//import com.outfood.common.common.HubSelectUtil;
//import com.ruoyi.common.exception.BusinessException;
//import lombok.Data;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections.CollectionUtils;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletResponse;
//import java.lang.reflect.Method;
//import java.util.List;
//import java.util.Random;
//
///**
// * @Author huisonglin
// * @Description
// * @PackageName hainan-rest
// * @Package com.guarantee.server.config.acpectj
// * @Date 2022/11/29 17:27
// * @Version 1.0
// */
//@Aspect
//@Slf4j
//@Component
//public class HubSelectAspect {
//
//    @Resource
//    PythonServerConfig pythonServerConfig;
//
//    public static List<String> hubUrls;
//
//    @PostConstruct
//    public void setHubUrls(){
//        hubUrls = pythonServerConfig.getLoginList();
//    }
//
//    //开始获取有效地址
//    public String getAvailableUrl(){
//        if(CollectionUtils.isNotEmpty(hubUrls)){
//            int index = new Random().nextInt(hubUrls.size());
//            String url = hubUrls.get(index);
//            hubUrls.remove(url);
//            return url;
//
//        }else{
//            throw  new BusinessException("当前登录系统繁忙，请稍后再试");
//        }
//    }
//
//    //开始回收地址
//    public void recoveryUrl(String url){
//        if(!hubUrls.contains(url)){
//            hubUrls.add(url);
//        }
//    }
//
//
//    @Pointcut("@annotation(com.outfood.annotation.HubSelect)")
//    public void annotationPointCut() {
//    }
//
//    @Around("annotationPointCut()")
//    public Object around(ProceedingJoinPoint point) throws Throwable {
//        MethodSignature signature = (MethodSignature)point.getSignature();
//        Method method = signature.getMethod();
//        HubSelect annotation = (HubSelect)method.getAnnotation(HubSelect.class);
//        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        String availableUrl = null;
//        HttpServletResponse response = requestAttributes.getResponse();
//        try {
//            if(annotation != null){
//                availableUrl = getAvailableUrl();
//                log.info("本次获取的有效hub地址为{}",availableUrl);
//                HubSelectUtil.setUrl(availableUrl);
//            }
//            Object proceed = point.proceed();
//            return proceed;
//
//        } catch (Throwable e) {
//                throw e;
//        }finally {
//            recoveryUrl(availableUrl);
//            HubSelectUtil.remove();
//        }
//
//    }
////    @AfterReturning(pointcut = "@annotation(responseEncryption)", returning = "restResponse")
////    public void doAfterReturning(JoinPoint joinPoint, ResponseEncryption responseEncryption, RestResponse restResponse)
////    {
////
////        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
////        HttpServletResponse response = requestAttributes.getResponse();
////        HainanResponse hainanResponse = new HainanResponse();
////        hainanResponse.setResultCode(restResponse.getCode());
////        hainanResponse.setResult(restResponse.isSuccess()?"success":"failure");
////        hainanResponse.setResultMsg(restResponse.getMessage());
////        hainanResponse.setResultData(restResponse.getData());
////        String body = SMUtils.encryptBySM2(JSON.toJSONString(hainanResponse), guaranteeConfig.getPublicKey2());
////        responseString(response,JSON.toJSONString(body));
////    }
//
//    @Data
//    public static class Response<T>{
//        private Integer code;
//        private String message;
//        private T data;
//    }
//
//
//
//
//}
